import { createSSRApp } from "vue";
import App from "./App";
import i18n from "./locale";
//时间插件
import moment from "moment";
import cuCustom from "@/public/colorui/components/cu-custom.vue";
// 下拉刷新组件 文档地址：http://www.mescroll.com/uni.html
// 在main.js注册全局组件
import MescrollBody from "@/components/mescroll-uni/mescroll-body.vue";
import MescrollUni from "@/components/mescroll-uni/mescroll-uni.vue";

function initApp(app) {
  uni.getSystemInfo({
    success: function (e) {
      // #ifndef MP
      app.config.globalProperties.StatusBar = e.statusBarHeight;
      if (e.platform == "android") {
        app.config.globalProperties.CustomBar = e.statusBarHeight + 50;
      } else {
        app.config.globalProperties.CustomBar = e.statusBarHeight + 45;
      }
      // #endif

      // #ifdef MP-WEIXIN
      app.config.globalProperties.StatusBar = e.statusBarHeight;
      let custom = wx.getMenuButtonBoundingClientRect();
      app.config.globalProperties.Custom = custom;
      app.config.globalProperties.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
      // #endif

      // #ifdef MP-ALIPAY
      app.config.globalProperties.StatusBar = e.statusBarHeight;
      app.config.globalProperties.CustomBar = e.statusBarHeight + e.titleBarHeight;
      // #endif
    },
  });
  // import moment from './public/moment/moment.js';
  app.config.globalProperties.$moment = moment; //赋值使用
  app.component("cu-custom", cuCustom);
  app.component("mescroll-body", MescrollBody);
  app.component("mescroll-uni", MescrollUni);
  app.config.globalProperties.$moneyFormat = function (val) {
    if (val) {
      val = val.toString().replace(/\$|\,/g, "");
    }
    if (isNaN(val)) {
      val = "0";
    }
    let sign = val == (val = Math.abs(val));
    val = Math.floor(val * 100 + 0.50000000001);
    let cents = val % 100;
    val = Math.floor(val / 100).toString();
    if (cents < 10) {
      cents = "0" + cents;
    }
    for (var i = 0; i < Math.floor((val.length - (1 + i)) / 3); i++) {
      val = val.substring(0, val.length - (4 * i + 3)) + "," + val.substring(val.length - (4 * i + 3));
    }
    return (sign ? "" : "-") + val + "." + cents;
  };
}

const app = createSSRApp(App);
export function createApp() {
  app.use(i18n);
  initApp(app);
  return {
    app,
  };
}
